//
//  OATableViewController.m
//  OASystem
//
//  Created by 刘沿 on 16/5/27.
//  Copyright © 2016年 zpf. All rights reserved.
//

#import "OATableViewController.h"
#import "MBProgressHUD.h"
#import "HttpTools.h"
#import "CountyDepartmentListModel.h"
#import "HeaderView.h"

@interface OATableViewController ()<HeaderViewDelegate>

@property (nonatomic,strong) NSMutableArray *contentsArray;

@property (nonatomic,strong) NSMutableArray *groupArray;

@property(nonatomic,strong)UIBarButtonItem * rightItem;


@end

@implementation OATableViewController

- (NSMutableArray *)contentsArray{
    if (_contentsArray == nil) {
        
        _contentsArray = [NSMutableArray array];
        
    }

    return _contentsArray;
}

- (NSMutableArray *)groupArray{
    if (_groupArray == nil) {
        
        _groupArray = [NSMutableArray array];
        
    }

    return _groupArray;

}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self setRightItem];

    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    NSArray *array = @[@{@"userId":userId}];
    
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    [HttpTools requestWithURL:KExchTree arrays:array method:KLocalTree success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSLog(@"%@",JSON);
        NSArray * data = JSON[@"data"];
        if (data.count) {
            NSDictionary * ditc = [data firstObject];

//存储的是所有数据
            [self.contentsArray addObjectsFromArray:[CountyDepartmentListModel dataWithArray:ditc[@"PartNode"]]];
            //存储组头
            self.groupArray = [self groups];
            
            for (CountyDepartmentListModel * model in self.groupArray) {
                NSMutableArray * subArray = [NSMutableArray array];
                for (CountyDepartmentListModel * listModel in self.contentsArray) {
                    if ([model.depid isEqualToString:listModel.depid] && listModel.bmmc.length ==0) {
                        [subArray addObject:listModel];
                        model.subArrays = subArray;
                    }
                }
            }

            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];

    
}

-(void)setRightItem{
    UIBarButtonItem * operationItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion)];
    self.navigationItem.rightBarButtonItem = operationItem;
    self.rightItem = operationItem;
    
}

//确定提交
- (void)operationACtion{

    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * model in self.contentsArray){
        if (model.isSelected) {
            [tempArray addObject:model.ryxm];
        }
        
    }
    if (tempArray.count == 0) {
        return;
    }

     
    if ([self.delegate respondsToSelector:@selector(TableViewController:array:)]) {
        
        [self.delegate TableViewController:self array:tempArray];
        
    }
    
    [self.navigationController popToViewController:self.navigationController.viewControllers[3] animated:YES];
    
}




-(NSMutableArray*)groups{
    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * listModel in self.contentsArray) {
        if (listModel.bmmc.length !=0) {
            [tempArray addObject:listModel];
        }
        
    }
    return tempArray;
}

-(void)clickHeaderView:(NSInteger)index{
    CountyDepartmentListModel * listModel = self.groupArray[index];
    listModel.isOpen = !listModel.isOpen;
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CountyDepartmentListModel * listModel = self.groupArray[section];
    return   listModel.isOpen ? listModel.subArrays.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellid = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
    CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    cell.textLabel.text = subModel.ryxm;
    if (subModel.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate = self;
    CountyDepartmentListModel * listModel = self.groupArray[section];
    headView.bgButton.tag = section;
    [headView.bgButton setTitle:listModel.bmmc forState:UIControlStateNormal];
    headView.bgButton.imageView.transform = listModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
    CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
    
    [self.tableView reloadData];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

@end
